package gui;

import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileOutputStream;

import javax.swing.Box;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;

import com.itextpdf.text.*;
import com.itextpdf.text.exceptions.IllegalPdfSyntaxException;
import com.itextpdf.text.pdf.*;

import commands.LoginCheckCommand;
import entities.Manager;

/**
 * 
 * @author Erin eks691 and sff803 Samuel Ferguson
 * 
 */
public class MainMenuFrame extends JFrame {

	/**
	 * 
	 */
	public JFileChooser fc;

	/** The standard width for the frame. */
	public static final int DEFAULT_WIDTH = 700;

	/** The standard height for the frame. */
	public static final int DEFAULT_HEIGHT = 700;

	/** Container to hold the panels for the program */
	private Container contentPane;

	/** id of either the student, manager, or employee in the system */
	private int id;
	private JFrame frame;

	/**
	 * Initialize the login window.
	 */
	public MainMenuFrame(LoginCheckCommand.UserType userType, int i) {
		fc = new JFileChooser();
		frame = this;
		id = i;
		setTitle("Student Loan System");
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		// setLocation(40, 120);
		contentPane = getContentPane();

		if (userType == LoginCheckCommand.UserType.STUDENT) {
			StudentChangeInfoPanel studentPanel = new StudentChangeInfoPanel(id);
			studentPanel.setMaximumSize(studentPanel.getPreferredSize());
			add(studentPanel);
			studentPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
		} else if (userType == LoginCheckCommand.UserType.EMPLOYEE) {
			// Employee Panel
			setJMenuBar(EmployeeMenuBar());
			EmployeeOptionPanel employeePanel = new EmployeeOptionPanel(id);
			employeePanel.setMaximumSize(employeePanel.getPreferredSize());
			add(employeePanel);
			employeePanel.setAlignmentX(Component.CENTER_ALIGNMENT);
		} else if (userType == LoginCheckCommand.UserType.MANAGER) {
			setTitle("Manager Panel by Samuel Ferguson");
			setSize(600, 400);
			this.setLocation(100, 100);
			setJMenuBar(createManagerMenuBar());
			// put up management panel
			changePanel(new ManagerCreateEmployeePanel());
		}

	}

	/**
	 * Menu bar for the employees - eks691
	 * 
	 * @return
	 */
	private JMenuBar EmployeeMenuBar() {
		JMenuBar menuBar;
		JMenu menu;
		JMenuItem menuItem;

		// Create the menu bar.
		menuBar = new JMenuBar();

		// Build the first menu.
		menu = new JMenu("Console");
		menuBar.add(menu);

		// a group of JMenuItems
		menuItem = new JMenuItem("Logout");
		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				LoginFrame frame = new LoginFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
				setVisible(false);
			}
		});
		menu.add(menuItem);

		// Build second menu in the menu bar.
		menu = new JMenu("Students");
		menuBar.add(menu);

		// a group of JMenuItems
		menuItem = new JMenuItem("New Loans");
		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				changePanel(new EmployeeNewLoanPanel(id));
			}
		});
		menu.add(menuItem);

		menuItem = new JMenuItem("Review Accounts");
		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				changePanel(new EmployeeAllStudentPanel(id));
			}
		});
		menu.add(menuItem);

		return menuBar;
	}

	/**
	 * @author Samuel Ferguson sff803
	 * @return A new menu.
	 */
	private JMenuBar createManagerMenuBar() {

		JMenuBar menuBar;
		JMenu menu;
		JMenuItem menuItem;

		// Create the menu bar.
		menuBar = new JMenuBar();

		// Build the first menu.
		menu = new JMenu("Console");
		menu.setMnemonic(KeyEvent.VK_O);
		menu.getAccessibleContext().setAccessibleDescription(
				"The only menu in this program that has menu items");
		menuBar.add(menu);

		// a group of JMenuItems
		menuItem = new JMenuItem("New Employee", KeyEvent.VK_N);
		// menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1,
				ActionEvent.ALT_MASK));
		menuItem.getAccessibleContext().setAccessibleDescription(
				"Create new employee");
		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				changePanel(new ManagerCreateEmployeePanel());
			}
		});
		menu.add(menuItem);

		// a submenu
		menu.addSeparator();

		// a group of JMenuItems
		menuItem = new JMenuItem("Logout", KeyEvent.VK_O);
		// menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
				ActionEvent.CTRL_MASK));
		menuItem.getAccessibleContext().setAccessibleDescription("Exit");
		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				LoginFrame frame = new LoginFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
				setVisible(false);
			}
		});
		menu.add(menuItem);

		// Build second menu in the menu bar.
		menu = new JMenu("Payouts");
		menu.setMnemonic(KeyEvent.VK_P);
		menu.getAccessibleContext().setAccessibleDescription(
				"Access monthly payouts");
		menuBar.add(menu);

		// a group of JMenuItems
		menuItem = new JMenuItem("Generate", KeyEvent.VK_G);
		// menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2,
				ActionEvent.ALT_MASK));
		menuItem.getAccessibleContext()
				.setAccessibleDescription("List payouts");
		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				changePanel(new GeneratePayoutsPanel());
			}
		});
		menu.add(menuItem);
		menuItem = new JMenuItem("Print as PDF", KeyEvent.VK_P);
		// menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2,
				ActionEvent.CTRL_MASK));
		menuItem.getAccessibleContext()
				.setAccessibleDescription("Print to PDF");
		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				File file;
				int returnVal = fc.showSaveDialog(MainMenuFrame.this);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					file = fc.getSelectedFile();
					// This is where a real application would save the file.

					pdfPrint(file);
				}
			}
		});
		menu.add(menuItem);
		// Build second menu in the menu bar.
		menu = new JMenu("Bills");
		menu.setMnemonic(KeyEvent.VK_B);
		menu.getAccessibleContext().setAccessibleDescription(
				"Access monthly billing info");
		menuBar.add(menu);

		// a group of JMenuItems
		menuItem = new JMenuItem("Generate", KeyEvent.VK_G);
		// menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3,
				ActionEvent.ALT_MASK));
		menuItem.getAccessibleContext().setAccessibleDescription(
				"Create new list of bills");
		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				changePanel(new GenerateBillsPanel());

			}
		});
		menu.add(menuItem);
		menuItem = new JMenuItem("Print as PDF", KeyEvent.VK_P);
		// menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3,
				ActionEvent.CTRL_MASK));
		menuItem.getAccessibleContext()
				.setAccessibleDescription("Print to PDF");
		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				File file;
				int returnVal = fc.showSaveDialog(MainMenuFrame.this);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					file = fc.getSelectedFile();
					// This is where a real application would save the file.

					pdfPrint(file);
				}
			}
		});
		menu.add(menuItem);
		// Build second menu in the menu bar.
		menu = new JMenu("Interest");
		menu.setMnemonic(KeyEvent.VK_I);
		menu.getAccessibleContext().setAccessibleDescription(
				"Modify interest rates");
		menuBar.add(menu);

		// a group of JMenuItems
		menuItem = new JMenuItem("Modify Interest Rate", KeyEvent.VK_M);
		// menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_4,
				ActionEvent.ALT_MASK));
		menuItem.getAccessibleContext().setAccessibleDescription(
				"Modify interest rate");
		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				changePanel(new ModifyInterestPanel());
			}
		});
		menu.add(menuItem);

		// Build second menu in the menu bar.
		menu = new JMenu("Students");
		menu.setMnemonic(KeyEvent.VK_S);
		menu.getAccessibleContext().setAccessibleDescription(
				"Access student info");
		menuBar.add(menu);

		menuBar.add(Box.createHorizontalGlue());

		// a group of JMenuItems
		menuItem = new JMenuItem("View list", KeyEvent.VK_V);
		// menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_5,
				ActionEvent.ALT_MASK));
		menuItem.getAccessibleContext().setAccessibleDescription(
				"View list of students");
		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				changePanel(new ListStudentsPanel());
			}
		});
		menu.add(menuItem);
		menuItem = new JMenuItem("Print as PDF", KeyEvent.VK_P);
		// menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_5,
				ActionEvent.CTRL_MASK));
		menuItem.getAccessibleContext()
				.setAccessibleDescription("Print to PDF");
		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				File file;
				int returnVal = fc.showSaveDialog(MainMenuFrame.this);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					file = fc.getSelectedFile();
					// This is where a real application would save the file.

					pdfPrint(file);
				}
			}
		});
		menu.add(menuItem);
		// Build second menu in the menu bar.
		menu = new JMenu("Help");
		menu.setMnemonic(KeyEvent.VK_H);
		menu.getAccessibleContext().setAccessibleDescription(
				"Access helpful info");
		menuBar.add(menu);

		// a group of JMenuItems
		menuItem = new JMenuItem("About Student Loan System", KeyEvent.VK_A);
		// menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_6,
				ActionEvent.ALT_MASK));
		menuItem.getAccessibleContext().setAccessibleDescription(
				"Create new employee");

		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				JOptionPane
						.showMessageDialog(
								frame,
								"  The Student Loan System v1.0.0\n  Manager Interface by Samuel Ferguson\n   If you liked this, give me a good mark!",
								"About", JOptionPane.PLAIN_MESSAGE);
			}
		});
		menu.add(menuItem);
		return menuBar;
	}

	/**
	 * Print to PDF Pass in a string for the file name
	 */
	private void pdfPrint(File fileName) {

		if (Manager.table != null) {
			Document document = new Document(PageSize.A4.rotate());
			try {
				PdfWriter writer = PdfWriter.getInstance(document,
						new FileOutputStream(fileName));

				document.open();
				document.addTitle("Loans for Students on Demand Student Loan System");
				document.add(new Paragraph(
						"Loans for Students on Demand Student Loan System"));

				PdfContentByte cb = writer.getDirectContent();

				cb.saveState();
				Graphics2D g2 = cb.createGraphicsShapes(500, 500);

				Shape oldClip = g2.getClip();
				g2.clipRect(0, 0, 500, 500);

				Manager.table.print(g2);
				// this.print(g2);
				g2.setClip(oldClip);

				g2.dispose();
				cb.restoreState();
			} catch (Exception e) {
				System.err.println(e.getMessage());
			}
			try {
				document.close();
			} catch (IllegalPdfSyntaxException e) {
				System.out.println(e.getMessage());
			}
		}
	}

	/**
	 * Change the panels in the frame
	 * 
	 * @param the
	 *            new panel to be displayed
	 */
	public void changePanel(JPanel newPanel) {
		contentPane.removeAll();
		contentPane.add(newPanel);
		// System.out.println("new panel created");//for debugging purposes
		validate();
		setVisible(true);
	}

	public static final long serialVersionUID = 1;
}
